exports.controllers={
	/*
		Function: controllers.guestbook_short
		
		
		Parameters:
			options - an object of:
			options.limit - optional, count of messages, defaults to 3
			options.template - optional, defaults to "guestbook/body"
			options.guestbook_id - optional, id from Tguestbooks
			options.guestbook_code - optional, code from Tguestbooks
			options.guestbook_name - optional, name from Tguestbooks
			options.pqlength - optional, maxmimum size of question, in symbols
			options.palength - optional, maximum size of answer, in symbols
		Returns:
			Valid HTML
	*/
	
	guestbook_short: function(space,options){
		var str;
		if (!options) options = {};
		if (!options.template) options.template = "guestbooks/list_short";
		var cmid = space.functions.get_guestbook_id(space,options);
		var messages = space.mfact.list("guestbook_message",{guestbook_id: cmid,limit: options.limit || 3, moderated: 1, order_by: "main.id desc"});
		space.messages = messages;	
		
		for(var i=0; i<messages.length; i++){
			str=space.messages[i].question;
			if(options.pqlength && str && str.length>options.pqlength ){
				str=str.substring(0,options.pqlength) ; 
				str+="..."
			};
			space.messages[i].questionShot=str;
			
			str=space.messages[i].answer;
			if(options.palength && str && str.length>options.palength ){
				str=str.substring(0,options.palength) ; 
				str+="..."
			}
			space.messages[i].answerShot=str;
		};
		return space.views.process("widgets/"+options.template,space);
	},
	
	/*
		Function: controllers.guestbook
		
		
		Parameters:
			options - an object of:
			options.template - optional, defaults to "guestbook/body"
			options.guestbook_id - optional, id from Tguestbooks
			options.guestbook_code - optional, code from Tguestbooks
			options.guestbook_name - optional, name from Tguestbooks
		Returns:
			Valid HTML
	*/
	
	guestbook: function(space,options) {
		if (!options) options = {};
		if (!options.template) options.template = "guestbooks/list";
		var cmid = space.functions.get_guestbook_id(space,options);
		options.guestbook_id = cmid;
		var mode = space.fields.mode || "view";
		return space.controllers["guestbook__"+mode](space,options);
	},
	
	
	guestbook__write: function(space,options) {
		var avatar_id=space.functions.user_upload_file(space,{inputname: "avatar",fg_code: "avatars",uid:space.uid||1,downscale_policy: {x:300,y:300}, order_by: "main.id desc"});
	
		var msg = space.mfact.from_object("guestbook_message",{
			guestbook_id: options.guestbook_id,
			user_a_id: null,
			user_q_avatar_id: avatar_id,
			user_q_name: space.fields.user_q_name,
			user_q_phone: space.fields.user_q_phone,
			user_q_email: space.fields.user_q_email,
			moderated: 0,
			question: space.fields.question
		});
		msg.Save();
	
		space.guestbook=space.mfact.get("guestbook",options.guestbook_id);
		space.message=msg;
		
		var email_body;
		space.site_name=space.site.names[0];
		if (space.guestbook.email_template) email_body=space.views.apply(space.guestbook.email_template,space); else email_body=space.views.process("widgets/guestbooks/email",space);
	
		if (space.guestbook.email) {
			var hash={
				from:"no-reply@"+space.site.names[0],
				to:space.guestbook.email.split(/\s*,\s*/)
			};
			var headers = {
				From: hash.from,
				"Content-Type": 'text/plain; charset="UTF-8"'
			};
			space.mail=hash;
			var subject=email_body.match(/^(.*?)\n/)[1];
			for (var i=0;i<hash.to.length;i++) {
				if (hash.to[i].match(/\w@\w/)) require("mail").mail(hash.to[i],subject,email_body,headers);
			}
		}
		return JSON.stringify({call:"guestbook_write_ok"});
	},
		
	guestbook__view: function(space,options) {
		var messages = [];
		var premoderation = space.mfact.get("guestbook",options.guestbook_id).premoderation;
		if (premoderation == 1)	{
			messages = space.mfact.list("guestbook_message",{guestbook_id: options.guestbook_id, moderated: premoderation, order_by: "main.id desc"});
		}else{
			messages = space.mfact.list("guestbook_message",{guestbook_id: options.guestbook_id, order_by: "main.id desc"});	
		};
		space.messages = messages;	
		var guestbook = space.mfact.get("guestbook",options.guestbook_id);
		space.guestbook = guestbook;
			
		for(var i=0; i<space.messages.length; i++){
			str=space.messages[i].question;
			if(str.length>options.pqlength ){
				str=str.substring(0,options.pqlength) ; 
				str+="..."
			};
			space.messages[i].questionShot=str;
			
			str=space.messages[i].answer;
			if(str && str.length>options.palength ){
				str=str.substring(0,options.palength) ; 
				str+="..."
			}
			space.messages[i].answerShot=str;
		};	
		return space.views.process("widgets/"+options.template,space);
	
		
		//return space.Dumper(messages["user_a_id"]);
	},
	
	
	
	admin_guestbook_messages: function(space) { 
		var mode = space.fields.mode || "choose";
		return space.controllers["admin_guestbook_messages__"+mode](space);
	},
	
	admin_guestbook_messages__choose: function (space) {
		var guestbooks = space.mfact.list("guestbook");
		space.guestbooks = guestbooks;
		return space.views.process("widgets/admin_guestbooks/choose",space);
	},
	
	admin_guestbook_messages__list: function (space) {
		var guestbook_id = space.fields.guestbook_id;
		var message_id = space.fields.message_id;
		var guestbook = space.mfact.get("guestbook",guestbook_id);
		var messages = space.mfact.list("guestbook_message", {guestbook_id: guestbook_id, order_by: "main.id desc"} ); 
		space.messages = messages;
		space.guestbook = guestbook;		
		return space.views.process("widgets/admin_guestbooks/list",space);
	},
	
	admin_guestbook_messages__edit: function (space) {
		
		var message_id = space.fields.message_id;
		var guestbook_id = space.fields.guestbook_id;
		
		var message;
		if (message_id) {
			message = space.mfact.get("guestbook_message", {guestbook_id: guestbook_id, id: message_id, order_by: "main.id desc"}); 
		} else {
			message={};
		}
		space.message = message;

		space.moderators=space.site.sql.execute_and_fetch("users/list_names");
		space.moderators.unshift({id:"",name:""});
		//throw new Error("message="+message.question);
		return space.views.process("widgets/admin_guestbooks/edit",space);
	},
	
	admin_guestbook_messages__save: function (space) {	
		var item;
		if (space.fields.message_id) {
			item=space.mfact.get("guestbook_message",space.fields.message_id);
			item.UpdateFromObject(space.fields);
		} else{
			item=space.mfact.from_object("guestbook_message",space.fields);
		}
		
		item.Save();
		return JSON.stringify({redirect:{url: space.action+"?mode=list&guestbook_id="+space.fields.guestbook_id}});	
	},
	
	admin_guestbook_messages__delete: function (space) {
		
		var item = space.mfact.get("guestbook_message",space.fields.message_id);
		item.Remove();
		//space.mfact.get("market_category",space.fields.id).Delete();
		
		
		return JSON.stringify({redirect:{url: space.action+"?mode=list&guestbook_id="+space.fields.guestbook_id}});
	}
};


exports.functions={
	/*
		Function: functions.get_guestbook_id

		Gets guestbook_id from either one - code, name or id of guestbook

		Parameters:
			options - the hash of:
			options.PREFIX_id - optional, from Tguestbooks table
			options.PREFIX_code - optional, from Tguestbooks table
			options.PREFIX_name - optional, from Tguestbooks table
			prefix - a prefix to use in search of options

		Returns:
			Numeric ID or undefined
	*/
	get_guestbook_id:function(space,options,prefix) {
		if (!prefix) prefix="guestbook";
		if (options[prefix+"_id"]) return options[prefix+"_id"];
		if (options[prefix+"_code"]) return space.site.sql.execute_and_fetch_one("guestbooks/get_by_code",{code: options[prefix+"_code"]}).id;
		if (options[prefix+"_name"]) return space.site.sql.execute_and_fetch_one("guestbooks/get_by_name",{name: options[prefix+"_name"]}).id;
		return undefined;
	}
};
